"use strict";
/* Controllers */
app.controller("zhibiaoDashboardController", ['$scope', '$location', '$rootScope', '$routeParams','zhibiaoDashboardService', function ($scope, $location, $rootScope, $routeParams,zhibiaoDashboardService) {



    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.biz = $routeParams.biz;
    //vm.biz = "zp";// [zp,ganji,chinahr]
    vm.result = {};
    vm.items=[];
    vm.ready = false;
    var errorFn = function (result) {
        alert(result);
    };
    $scope.listZhibiaoDashboard = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {dt:getDateStr(-30), dt2:getDateStr(-1), biz: vm.biz};
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            var dapan = {};
            for(var i=0;i<data.dapan.length; ++i) {
                var item = data.dapan[i];
                // 将数组转化为对象
                dapan[item.metric] = updownFormatter(item.content);
            }
            vm.result.dapan = dapan;
        };
        zhibiaoDashboardService.listZhibiaoDashboard(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        //var $resultTable = $('#list_table');
        //$resultTable.bootstrapTable({
        //     pageList: [10, 30, 50, 100, 300, 500, 1000], pageSize: 30
        //});
        $scope.listZhibiaoDashboard();
    };
    $scope.initializeController();

}]);

/* Services */
app.service('zhibiaoDashboardService', ['ajaxService', function (ajaxService) {

    this.listZhibiaoDashboard = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/dapan/listZhibiaoDashboard.do", successFunction, errorFunction);
    };
}]);


function updownFormatter(value) {
    if (value == null) {
        return "-";
    }
    var items = (value+'').split(',');
    var val = items[0];
    var diff = parseFloat(items[1]);

    var str='';
    if (diff < 0) {
        str = '<span style="color:green">' + percentFmt(diff) + '</span>' + '<span style="color:green" class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>';
    } else if (diff > 0) {
        str ='<span style="color:red">' + percentFmt(diff) + '</span>'+ '<span style="color:red" class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>';
    } else {
        str = '-';
    }
    return val + '(' + str +')';
    //return str;
}
function percentFmt(diff) {
    if(diff==null) return '-';
    return diff*100 + '%';
}
